<?php
/**
 * 公共控制器
 */
namespace app\index\controller;
use think\Controller;
class Common extends Controller{

    public function index(){
    	
        //展示视图
		return $this->fetch();
    }

    /**
     * 设置页面信息
     * @param string $title     标题
     * @param string $keyword   关键字
     * @param string $desc      描述 
     * @param array  $css       css列表   
     * @param array  $js        js列表
     * @return void
     */
    public function setPageMsg( $title = '', $keyword = '', $desc = '', $css = [], $js = [] ){
    	
        //分配变量
    	$this->assign([
    		'title' => $title,
    		'keyword' => $keyword,
    		'description' => $desc,
    		'style' => $css,
    		'js' => $js,
    	]);
        
    }

    /**
     * 获取无限极分类
     */
    public function getUnlimitedCats(){
        
        // 从缓存中获取数据
        $cats = cache('limitCat');

        // 如果缓存中没有就从数据库汇总获取
        if( !$cats ){
            
            $data = db('category')->select();
            $cats = [];
            foreach ($data as $k => $v) {

                //一级分类
                if( $v['parent_id'] == 0) {

                    $cats[$k] = $v;
                    $cats[$k]['child'] = [];//默认是空数组
                    foreach ($data as $k1 =>$v1) {

                        //二级分类
                        if( $v1['parent_id'] == $v['cid']) {

                            $cats[$k]['child'][$k1] = $v1;
                            $cats[$k]['child'][$k1]['child'] = [];//默认是空数组

                            foreach ($data as $k2 =>$v2) {

                                //三级分类
                                if( $v2['parent_id'] == $v1['cid']) {

                                    $cats[$k]['child'][$k1]['child'][$k2] = $v2;

                                }

                            }
                            
                        }

                    }
                    
                }
                
            }

            // 设置缓存
            cache('limitCat', $cats, 3600);

        }

        // 返回分类
        return $cats;
       
    }

    /**
     * 获取无限极分类
     */
    public function loginRedirect(){

        if( input('act') == 'deletecache' ){

            $msg = '';

        }else{

            $msg = '/';

        }

        // 将要返回的页面地址存入session中
        session('returnUrl',$msg.input('returnUrl'));

        echo json_encode(['status'=>true]);

    }

    /**
     * AJAX判断登录状态
     */
    public function checkLogin(){

        // 
        if( session('mid') ){

            echo json_encode( ['status'=>true , 'nickname'=>session('nickname'), 'logout'=>url("Member/logout")] );

        }else {

            echo json_encode( ['status'=>false , 'login'=>url("Member/loginShow"), 'regist'=>url("Member/registShow")] );

        }

    }








}
